package io.jft.doll.model.viewModel

import android.arch.lifecycle.MutableLiveData
import com.g.base.extend.switchMap
import com.g.base.viewModel.BaseViewModel
import com.google.gson.JsonObject
import io.jft.doll.room.repository.RoomRepository

/**
 * Created by G on 2017/12/21 0021.
 */
class RoomListViewModel : BaseViewModel() {
    private val roomRepository by lazy { RoomRepository() }
    private val operateRoomList by lazy { MutableLiveData<JsonObject>() }
    val limit = 20
    var lastRequestSize = 0

    fun operateRoomList(offset: Int, groupId: String? = null, limit: Int? = null) {
        lastRequestSize = offset + (limit ?: this.limit)
        operateRoomList.postValue(
                JsonObject().apply {
                    addProperty("offset", offset)
                    addProperty("limit", limit ?: this@RoomListViewModel.limit)
                    if (groupId != null) addProperty("groupId", groupId)
                }
        )
    }

    fun obsRoomList() = operateRoomList.switchMap { roomRepository.getRoomList(it) }
}

